package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@Tag(name = "02-车辆模块", description = "20")
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "brand", description = "品牌名称", required = true),
            @Parameter(name = "license", description = "车辆牌照", required = true),
            @Parameter(name = "model", description = "车辆型号", required = true),
            @Parameter(name = "code", description = "车辆识别代码", required = true),
            @Parameter(name = "displacement", description = "车辆排量", required = true),
            @Parameter(name = "type", description = "车辆类型", required = true),
            @Parameter(name = "color", description = "车身颜色", required = true),
            @Parameter(name = "batteryType", description = "电池类型", required = true),
            @Parameter(name = "vehicleAddParam", hidden = true)
    })
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");

        log.debug("vehicleAddParam=" + vehicleAddParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());

        log.debug("开始添加车辆对象：" + vehicle);
        vehicleMapper.insertVehicle(vehicle);

        return JsonResult.ok();
    }

    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {

            @Parameter(name = "brand", description = "车辆品牌"),
            @Parameter(name = "license", description = "车辆牌照"),
            @Parameter(name = "vehicleListQuery", hidden = true),
    })
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery, HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");

        log.debug("vehicleListQuery = " + vehicleListQuery);
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVOS = vehicleMapper.selectVehicleList(vehicleListQuery, userId);
        return JsonResult.ok(vehicleListVOS);
    }

    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@Range(min = 1,max = 500, message = "编号在1~500之间") @PathVariable Long id, HttpSession session) {
        if(id<=0){
            throw new IllegalArgumentException("id值不能小于0");
        }
        UserVO userVO = (UserVO) session.getAttribute("user");

        log.debug("id = " + id);
        Long userId = userVO.getId();
        int rows = vehicleMapper.deleteVehicle(id, userId);
            return JsonResult.ok();
    }

}
